import pandas as pd
import numpy as np


def get3weight(datafile):

    A = np.array(pd.read_excel(datafile, header=None))
    row, col = A.shape
    x2 = np.mean(A, axis=0)
    s = np.std(A, axis=0)

    # 求复相关系数法计算出的权重weight_correlation(i)
    # 计算相关系数的标准化矩阵
    standard_martix_for_corrlation = np.zeros((row, col))
    for j in range(col):
        for i in range(row):
            standard_martix_for_corrlation[i, j] = (A[i, j]-x2[j])/s[j]

    # 计算矩阵standard_martix_for_corrlation的相关系数矩阵R
    R = np.corrcoef(standard_martix_for_corrlation, rowvar=False)

    # 计算第1、7个指标的复相关系数的过程：
    # 初始化矩阵：
    R_revise = np.zeros((col-1, col-1))
    rm_follow = np.zeros((1, col-1))
    rm = np.zeros((col-1, 1))

    # 第1个指标的复相关系数计算过程：
    R_revise[:, :] = np.linalg.inv(R[1:col, 1:col])
    # 计算rm'矩阵的值：
    rm_follow[:, :] = R[0, 1:col]
    # 计算rm矩阵的值：
    rm[:, :] = R[1:col, 0].reshape(col-1, 1)
    k = np.zeros((1, col))
    k[0, 0] = np.mat(rm_follow[:, :])*np.mat(R_revise[:, :])*np.mat(rm[:, :])

    # 第7个指标的复相关系数计算过程：
    R_revise[:, :] = np.linalg.inv(R[0:col-1, 0:col-1])
    rm_follow[:, :] = R[col-1, 0:col-1]
    rm[:, :] = R[0:col-1, col-1].reshape(col-1, 1)
    k[0, col-1] = np.mat(rm_follow[:, :])*np.mat(R_revise[:, :])*np.mat(rm[:, :])

    # 求第2-6个指标的复相关系数：
    temp = np.zeros((col-1, col-1))
    temp_rm_follow = np.zeros((1, col-1))
    temp_rm = np.zeros((col-1, 1))
    for i in range(1, col):
        temp = np.mat(temp)*0
        # 求矩阵Rm-1的值：
        temp[0:i - 1, 0:i - 1] = R[0:i - 1, 0:i - 1]
        temp[0:i - 1, i - 1:col - 1] = R[0:i - 1, i:col]
        temp[i - 1:col - 1, 0:i - 1] = R[i:col, 0:i - 1]
        temp[i - 1:col - 1, i - 1:col - 1] = R[i:col, i:col]
        R_revise[:, :] = np.linalg.inv(temp)
        # 求矩阵rm'的值：
        temp_rm_follow = np.mat(temp_rm_follow)*0
        temp_rm_follow[0, 0:i-1] = R[i-1, 0:i-1]
        temp_rm_follow[0, i-1:col-1] = R[i-1, i:col]
        rm_follow[:, :] = temp_rm_follow
        # 求矩阵rm的值：
        temp_rm = np.mat(temp_rm)*0
        temp_rm[0:i - 1, 0] = R[0:i - 1, i-1].reshape(i - 1, 1)
        temp_rm[i - 1:col - 1, 0] = R[i:col, i - 1].reshape(col - i, 1)
        rm[:, :] = temp_rm
        k[0, i-1] = np.mat(rm_follow[:, :])*np.mat(R_revise[:, :])*np.mat(rm[:, :])

    sum_correlation = 0
    for i in range(col):
        sum_correlation = sum_correlation + 1 / k[0, i]

    weight_correlation = np.zeros((1, col))
    for i in range(col):
        weight_correlation[0, i] = (1 / k[0, i]) / sum_correlation

    # 求熵权法计算出的权重weight_entropy(i)
    mink = np.min(standard_martix_for_corrlation, axis=0)
    method2_matrix = np.zeros((row, col))
    for j in range(col):
        if mink[j] < 0:
            for i in range(row):
                method2_matrix[i, j] = standard_martix_for_corrlation[i, j] - mink[j] + 0.0001

    # 基于熵值求解权值的方法
    sum_entropy = 0
    rr = np.zeros((row, col))
    e = np.zeros((1, col))
    for j in range(col):
        s = 0
        for i in range(row):
            rr[i, j] = np.mat(method2_matrix[i, j])*np.mat(np.log(method2_matrix[i, j]))
            s = s + rr[i, j]
        e[0, j] = -(1/np.log(row))*s
        sum_entropy = sum_entropy + (1 - e[0, j])
    weight_entropy = np.zeros((1, col))
    for j in range(col):
        weight_entropy[0, j] = (1 - e[0, j]) / sum_entropy

    # 求均值法计算出的权重weight_average(j)
    sum_average = 0
    weight_average = np.zeros((1, col))
    for i in range(col):
        sum_average = sum_average + x2[i]
    for j in range(col):
        weight_average[0, j] = x2[j] / sum_average

# 三种方法计算得到的指标权重。
    W = np.concatenate((weight_correlation, weight_entropy, weight_average), axis=0)
    return W


